﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using XML;
using FileSystem;
using ICSharpCode.SharpZipLib;
namespace Application_Packager
{
    class Program
    {


        static void Main(string[] args)
        {
            XML.XML tehXML = new XML.XML();
            FileSystem.Dir dir = new FileSystem.Dir();
            ICSharpCode.SharpZipLib.Zip.FastZip fastZip = new ICSharpCode.SharpZipLib.Zip.FastZip();

            string App_Name;
            string App_Author;
            string App_Description;
            string tDir = "";
            string dDir;
            string aExE;
            string[] path;

            Console.WriteLine("=========================================");
            Console.WriteLine(" PureUSB(TM) Application Packager v1.0.0 ");
            Console.WriteLine("   Vaccination X                         ");
            Console.WriteLine("=========================================");
            Console.WriteLine("");

            Console.Write("Application Name: ");
            App_Name = Console.ReadLine();
            Console.Write("Application Author: ");
            App_Author = Console.ReadLine();
            Console.Write("Application Description: ");
            App_Description = Console.ReadLine();

            Console.Write("Target Executable: ");
            aExE = Console.ReadLine();

            while (Directory.Exists(tDir) == false)
            {
                Console.Write("Target Directory: ");
                tDir = Console.ReadLine();
                if (Directory.Exists(tDir) == false)
                {
                    Console.WriteLine("");
                    Console.WriteLine("That Directory does not exist!!!");
                    Console.WriteLine("");
                }
            
            }

            Console.Write("Output Directory: ");
            dDir = Console.ReadLine();

            Console.WriteLine("----------------------------");

            Console.WriteLine("Creating Directories...");
            Directory.CreateDirectory(dDir);
            Directory.CreateDirectory(dDir + "\\" + App_Name + ".app");

            Console.WriteLine("Copying Files...");
            path = tDir.Split('\\');
            dir.Copy_Tree(tDir, dDir + "\\" + App_Name + ".app\\", true, true);
            dir.Rename_Directory(dDir + "\\" + App_Name + ".app\\" + path[path.Length -1], "bin");
            
            Console.WriteLine("Writing Application Data...");
            tehXML.Load_XML(dDir + "\\" + App_Name + ".app\\Application.settings", "Settings/");
            tehXML.SetSetting("Assembly/Version", "1.0.0.0");
            tehXML.SetSetting("Assembly/EXE", aExE);
            tehXML.SetSetting("Application/Name", App_Name);
            tehXML.SetSetting("Application/Author", App_Author);
            tehXML.SetSetting("Application/Description", App_Description);
            
            Console.WriteLine("Compressing Application...");
            fastZip.CreateZip(dDir + "\\" + App_Name + ".tmp", dDir + "\\" + App_Name +".app", true,"");
            Directory.Delete(dDir + "\\" + App_Name + ".app", true);
            File.Copy(dDir + "\\" + App_Name + ".tmp", dDir + "\\" + App_Name + ".app");
            File.Delete(dDir + "\\" + App_Name + ".tmp");
            
            Console.WriteLine("Complete!");
        }
    }
}
